﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Xml;
using Insteon;
using Scheduling;
using Interfaces;
using Mesne;

namespace MesneConsole
{
    public class Program
    {
        static void Main(string[] args)
        {
            Callback cback = new Callback();
            
            var channel = DuplexChannelFactory<IServiceContract>.CreateChannel(cback, new Mesne.MesneBinding(), new EndpointAddress("net.tcp://server:34560/tcp"));
            ((IClientChannel)channel).Open();
            channel.EstablishCallback();
            List<IDevice> ds =  channel.GetDevices();

            IEnumerable<Switch> switches = ds.Where(x => x is Switch).Cast<Switch>();
            IEnumerable<Dimmer> dimmers = ds.Where(x => x is Dimmer).Cast<Dimmer>();
            IEnumerable<Thermostat> thermostats = ds.Where(x => x is Thermostat).Cast<Thermostat>();

            Switch christmasTree = switches.Where(x => x.Name == "Tree" && x.Location == "Den").FirstOrDefault();
            Dimmer denLight = dimmers.Where(x => x.Name == "Light" && x.Location == "Den").FirstOrDefault();
            Dimmer masterLight = dimmers.Where(x => x.Name == "Light" && x.Location == "Master").FirstOrDefault();
            Dimmer kidsLight = dimmers.Where(x => x.Name == "Light" && x.Location == "Kids").FirstOrDefault();
            Switch denFan = switches.Where(x => x.Name == "Fan" && x.Location == "Den").FirstOrDefault();
            Switch masterFan = switches.Where(x => x.Name == "Fan" && x.Location == "Master").FirstOrDefault();
            Switch kidsFan = switches.Where(x => x.Name == "Fan" && x.Location == "Kids").FirstOrDefault();

            channel.SendMessage(denLight.Off());

            //channel.ScheduleOperation
            //    (
            //        new Operation()
            //        {
            //            Name = "Testing5",
            //            Years = new TimeSpec("2014"),
            //            Months = new TimeSpec("2"),
            //            Hours = new TimeSpec("23"),
            //            Minutes = new TimeSpec("34"),
            //            Commands = new List<ICommand>() 
            //            { 
            //                christmasTree.Off() 
            //            }
            //        }
            //    );

            channel.DeleteAllOperations();

            //channel.ScheduleOperation
            //    (
            //        new Operation()
            //        {
            //            Name = "EverythingOffNight",
            //            DaysOfWeek = new TimeSpec("0,1,2,3,4,5,6"),
            //            Hours = new TimeSpec("22"),
            //            Minutes = new TimeSpec("55"),
            //            Commands = new List<ICommand>() 
            //            { 
            //                masterLight.Off(), 
            //                denLight.Off(),
            //                kidsLight.Off(),
            //                masterFan.Off(),
            //                denFan.Off(),
            //                kidsFan.Off()
            //            }
            //        }
            //    );

            //channel.DeleteOperation("EverythingOffNight");

            //channel.DeleteAllOperations();

            List<IOperation> ops = channel.GetOperations();
            //channel.DeleteOperation("EverythingOffMorning");
            //ops = channel.GetOperations();

            
            //channel.ScheduleOperation
            //    (
            //        new Operation()
            //        {
            //            Name = "Op2"
            //        }
            //    );

            //channel.ScheduleOperation
            //    (
            //        new Operation()
            //        {
            //            Name = "Op3"
            //        }
            //    );

            //channel.ScheduleOperation
            //    (
            //        new Operation()
            //        {
            //            Name = "Op4"
            //        }
            //    );

            //Console.WriteLine();

            //Console.WriteLine("Starting Operations:");
            //foreach (Operation op in channel.GetOperations())
            //{
            //    Console.WriteLine(op.Name);
            //}

            //Console.WriteLine();

            //Console.WriteLine("Deleting Operations (Op1, Op2, Op4)");
            //bool b =channel.DeleteOperations(new List<string> { "Op1", "Op2", "Op4" });

            //Console.WriteLine();

            //Console.WriteLine("Remaining Operations");
            //foreach (Operation op in channel.GetOperations())
            //{
            //    Console.WriteLine(op.Name);
            //}

            //Console.WriteLine();

            //Console.WriteLine("Add additional Operations (Kateshia, A'sharia)");
            //List<Operation> ops = new List<Operation>() { new Operation() { Name = "Kateshia" }, new Operation() { Name = "A'sharia" } };
            //bool c = channel.ScheduleOperations(ops);

            //Console.WriteLine();

            //Console.WriteLine("Ending Set:");
            //List<Operation> opers = channel.GetOperations();
            //foreach (Operation op in opers)
            //{
            //    Console.WriteLine(op.Name);
            //}

            //channel.DeleteAllOperations();
            //foreach (Operation op in channel.GetOperations())
            //{
            //    Console.WriteLine(op.Name);
            //}


            //for (int i = 0; i < 2; i++)
            //{
            //    channel.SendMessage(christmasTree.On());
            //    channel.SendMessage(christmasTree.Off());
            //}

            //System.Threading.Thread.Sleep(150);
            //channel.SendMessage(masterLight.Off());
            //System.Threading.Thread.Sleep(150);
            //channel.SendMessage(kidsLight.Off());
            //System.Threading.Thread.Sleep(150);

            //channel.SendMessage(denLight.LevelOn(0));
            //channel.SendMessage(denLight.OnFast());




            //channel.SendMessage(christmasTree.Off());
            //Operation op = new Operation();
            //op.Months = new List<int>() { 1 };
            //op.Hours = new List<int>() { 3 };
            //op.Minutes = new List<int>() { 16 };
            //Insteon.Command cmd = christmasTree.On();
            //op.Commands.Add(cmd);
            //op.Name = "TreeOn21";

            //Operation op1 = new Operation();
            //op1.Months = new List<int>() { 1 };
            //op1.Hours = new List<int>() { 3 };
            //op1.Minutes = new List<int>() { 18 };
            //Insteon.Command cmd1 = christmasTree.Off();
            //op1.Commands.Add(cmd1);
            //op1.Name = "TreeOff21";

            //Console.WriteLine(channel.ScheduleOperations(new List<Operation>() { op, op1 }));
            //Console.WriteLine(DateTime.Now.Month);


            //List<Operation> ops = channel.GetOperations();

            ////channel.SendMessage(dmr.LevelOnMessage(208));
            ////System.Threading.Thread.Sleep(2000);
            ////channel.SendMessage(dmr.OffMessage());
            ////System.Threading.Thread.Sleep(2000);
            ////Console.WriteLine("Response: " + channel.SendMessage(new Insteon.Message() { HexString = "02 62 0E 4C 8A 05 19 00" }));
            ////System.Threading.Thread.Sleep(1000);
            ////Console.WriteLine("Response: " + channel.SendMessage(new Insteon.Message() { HexString = Insteon.Commands.GetNextDatabaseEntry }));
            ////System.Threading.Thread.Sleep(1000);
            ////Console.WriteLine("Response: " + channel.SendMessage(new Insteon.Message() { HexString = Insteon.Commands.GetNextDatabaseEntry }));
            ////System.Threading.Thread.Sleep(1000);
            ////Console.WriteLine("Response: " + channel.SendMessage(new Insteon.Message() { HexString = Insteon.Commands.GetNextDatabaseEntry }));
            ////System.Threading.Thread.Sleep(1000);
            ////Console.WriteLine("Response: " + channel.SendMessage(new Insteon.Message() { HexString = Insteon.Commands.GetNextDatabaseEntry }));
            
            
            Console.ReadLine();
            if ((channel as IClientChannel).State == CommunicationState.Opened)
            {
                channel.Disconnect();
            }
            //(channel as IClientChannel).Abort();
        }
    }

    public class Callback : IClientCallback
    {
        public void Response(string message)
        {
            Console.WriteLine("Response: " + message);
        }

        public string SessionId
        {
            get;
            set;
        }
    }
}
